package org.lojzes.lojzesframework.factory;
import java.lang.annotation.Annotation;

import org.lojzes.lojzesframework.annotation.PreDao;
import org.lojzes.lojzesframework.handler.PreDaoHandler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;

public class PreDaoBeanScannerConfig implements BeanDefinitionRegistryPostProcessor{
	 
	private String basePackages;
	private boolean showSql;
	private Class<? extends Annotation> preDaoAnnotation = PreDao.class;
	
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		
	}
    /**
     * 注册bean
     */
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		//1.注册代理类
		 registerProxyHandler(registry);
		//2.扫描制定注解的类
		 PreDaoClassPathBeanScanner scanner = new PreDaoClassPathBeanScanner(registry, preDaoAnnotation);
		 scanner.doScan(basePackages);
	}

	private void registerProxyHandler(BeanDefinitionRegistry registry) {
		//代码注册
		GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
		genericBeanDefinition.setBeanClass(PreDaoHandler.class);
		genericBeanDefinition.getPropertyValues().add("showSql", showSql);
		registry.registerBeanDefinition("preDaoHandler", genericBeanDefinition);
		
	}
	public String getBasePackages() {
		return basePackages;
	}
	public void setBasePackages(String basePackages) {
		this.basePackages = basePackages;
	}
	public boolean isShowSql() {
		return showSql;
	}
	public void setShowSql(boolean showSql) {
		this.showSql = showSql;
	}
	public Class<? extends Annotation> getPreDaoAnnotation() {
		return preDaoAnnotation;
	}
	public void setPreDaoAnnotation(Class<? extends Annotation> preDaoAnnotation) {
		this.preDaoAnnotation = preDaoAnnotation;
	}

}
